package com.example.restatr.Atcivity.CitySubway;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.restatr.Atcivity.CitySubway.Bean.SevenDayWeatherBean;
import com.example.restatr.R;
import com.example.restatr.Tools;
import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.Description;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.formatter.ValueFormatter;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

public class WeatherAt extends AppCompatActivity {
    private Toolbar toolbar;
    private TextView barTitle;
    private EditText search;
    private TextView temp;
    private TextView weather;
    private TextView text1;
    private TextView text1_1;
    private TextView text2;
    private TextView text3;
    private TextView text4;
    private RecyclerView rv;
    private TextView dressingIndex;
    private TextView uvIndex;
    private TextView sportIndex;
    private TextView washIndex;
    private TextView weatherIndex;
    private TextView coldIndex;
    private TextView updateTime;
    private List<SevenDayWeatherBean.DataBean.TodayBean.HoursBean> hours = new ArrayList<>();
    private List<SevenDayWeatherBean.DataBean.WeatherListBean> weatherList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_weather_at);
        toolbar = findViewById(R.id.toolbar);
        barTitle = findViewById(R.id.bar_Title);
        search = findViewById(R.id.search);
        temp = findViewById(R.id.temp);
        weather = findViewById(R.id.weather);
        text1 = findViewById(R.id.text1);
        text1_1 = findViewById(R.id.text1_1);
        text2 = findViewById(R.id.text2);
        text3 = findViewById(R.id.text3);
        text4 = findViewById(R.id.text4);
        rv = findViewById(R.id.rv);
        dressingIndex = findViewById(R.id.dressingIndex);
        uvIndex = findViewById(R.id.uvIndex);
        sportIndex = findViewById(R.id.sportIndex);
        washIndex = findViewById(R.id.washIndex);
        weatherIndex = findViewById(R.id.weatherIndex);
        coldIndex = findViewById(R.id.coldIndex);
        updateTime = findViewById(R.id.updateTime);
        Tools.SetBarTitle(toolbar,barTitle,"天气信息",search,this,false);
        rv.setLayoutManager(new LinearLayoutManager(this, RecyclerView.HORIZONTAL, false));
        GetSevenDayWeather();
    }


    private void GetSevenDayWeather() {
        new Thread(() -> {
            String get = Tools.Get("/prod-api/api/common/weather", "");
            SevenDayWeatherBean sevenDayWeatherBean = new Gson().fromJson(get, SevenDayWeatherBean.class);
            SevenDayWeatherBean.DataBean.TodayBean today = sevenDayWeatherBean.getData().getToday();
            SevenDayWeatherBean.DataBean.TodayBean.ComfortLevelBean comfortLevel = today.getComfortLevel();
            SevenDayWeatherBean.DataBean.TodayBean.TempInfoBean tempInfo = today.getTempInfo();
            SevenDayWeatherBean.DataBean.TodayBean.WindBean wind = today.getWind();
            weatherList = sevenDayWeatherBean.getData().getWeatherList();
            hours = today.getHours();
            runOnUiThread(() -> {
                temp.setText(tempInfo.getTemperature() + "℃");
                weather.setText(tempInfo.getWeather());
                text1.setText(wind.getWindStrength());
                text1_1.setText(wind.getWindDirection());
                text2.setText(tempInfo.getHumidity() + "%");
                text3.setText(tempInfo.getApparentTemperature() + "°");
                text4.setText(tempInfo.getAir());
                dressingIndex.setText(comfortLevel.getDressingIndex());
                uvIndex.setText("紫外线较" + comfortLevel.getUvIndex());
                sportIndex.setText(comfortLevel.getSportIndex() + "户外运动");
                washIndex.setText(comfortLevel.getWashIndex() + "洗车");
                weatherIndex.setText(setWeatherIndex(tempInfo.getWeather()));
                coldIndex.setText(comfortLevel.getColdIndex() + "感冒");
                updateTime.setText("数据更新于" + today.getUpdateTime());
                rv.setAdapter(new adapter());

            });
        }).start();
    }


    private String setWeatherIndex(String washIndex) {
        String jg = null;
        switch (washIndex) {
            case "晴":
                jg = "不用带伞";
                break;
        }
        return jg;
    }

    public void OnClick(View view) {
    }

    public void onClick(View view) {
        Intent intent = new Intent(WeatherAt.this, SevenWeatherAt.class);
        Log.d("TAG", "onClick: " + weatherList);
        intent.putExtra("data", new Gson().toJson(weatherList));
        startActivity(intent);
    }

    class adapter extends RecyclerView.Adapter<adapter.viewHolder> {

        @NonNull
        @Override
        public viewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View inflate = getLayoutInflater().inflate(R.layout.hours_weather_layout, parent, false);
            return new viewHolder(inflate);
        }

        @Override
        public void onBindViewHolder(@NonNull viewHolder holder, int position) {
            holder.hour.setText(hours.get(position).getHour());
            holder.temp.setText(hours.get(position).getTemperature() + "℃");
            if (hours.get(position).getWeather().equals("晴")) {
                holder.weather.setImageResource(R.drawable.ic_round_wb_sunny_24);
            }
        }

        @Override
        public int getItemCount() {
            return hours.size();
        }

        class viewHolder extends RecyclerView.ViewHolder {
            private TextView hour;
            private ImageView weather;
            private TextView temp;

            public viewHolder(@NonNull View itemView) {
                super(itemView);
                hour = itemView.findViewById(R.id.hour);
                weather = itemView.findViewById(R.id.weather);
                temp = itemView.findViewById(R.id.temp);
            }
        }
    }


}